package com.yq.cp.service.basics.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.entity.basics.*;
import com.yq.cp.entity.permission.User;
import com.yq.cp.mapper.basics.*;
import com.yq.cp.mapper.permission.UserMapper;
import com.yq.cp.service.basics.InspectionTaskManagementService;
import com.yq.cp.utils.BeanCopyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 巡检任务管理 服务实现类
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@Service
public class InspectionTaskManagementServiceImpl extends ServiceImpl<InspectionTaskManagementMapper, InspectionTaskManagement> implements InspectionTaskManagementService {
    @Resource
    InspectionTaskManagementMapper inspectionTaskManagementMapper;
    @Resource
    PatrolPointMapper patrolPointMapper;
    @Resource
    InspectionPointItemMapper inspectionPointItemMapper;
    @Resource
    InspectionItemManagementMapper inspectionItemManagementMapper;
    @Resource
    InspectionPointManagementExtendMapper inspectionPointManagementExtendMapper;
    @Resource
    InspectionItemManagementExtendMapper inspectionItemManagementExtendMapper;
    @Resource
    PersonnelRelationsMapper personnelRelationsMapper;
    @Resource
    MessageMapper messageMapper;
    @Resource
    InspectionRoutePointMapper inspectionRoutePointMapper;
    @Resource
    InspectionPointManagementMapper inspectionPointManagementMapper;
    @Resource
    PatrolRouteManagementMapper patrolRouteManagementMapper;
    @Resource
    OrganizationalStructureMapper organizationalStructureMapper;
    @Resource
    UserMapper userMapper;
    @Override
    public void realAdd(InspectionTaskManagement vo) {
        vo.setState(0);
        PatrolRouteManagement patrolRouteManagement = patrolRouteManagementMapper.selectById(vo.getRouteId());
        vo.setRouteName(patrolRouteManagement.getName());
        OrganizationalStructure structure = organizationalStructureMapper.selectById(vo.getSiteId());
        vo.setSiteName(structure.getName());
        User user = userMapper.selectById(vo.getHeadId());
        vo.setHeadName(user.getNickname());
        inspectionTaskManagementMapper.insert(vo);
        List<InspectionRoutePoint> patrolPoints1 = inspectionRoutePointMapper.selectList(new QueryWrapper<InspectionRoutePoint>()
                .eq("route_id", vo.getRouteId()));
        List<Long> collect = patrolPoints1.stream().map(InspectionRoutePoint::getPointId).collect(Collectors.toList());
        List<InspectionPointManagement> patrolPoints = inspectionPointManagementMapper.selectBatchIds(collect);
        for (InspectionPointManagement patrolPoint : patrolPoints) {
            InspectionPointManagementExtend inspectionPointManagementExtend = new InspectionPointManagementExtend();
            inspectionPointManagementExtend.setName(patrolPoint.getName());
            inspectionPointManagementExtend.setPlanId(vo.getId());
            inspectionPointManagementExtend.setAssociatedDevicesId(patrolPoint.getId());
            inspectionPointManagementExtend.setState(0);
            inspectionPointManagementExtend.setType(patrolPoint.getType());
            inspectionPointManagementExtendMapper.insert(inspectionPointManagementExtend);
            List<InspectionPointItem> items = inspectionPointItemMapper.selectList(new QueryWrapper<InspectionPointItem>()
                    .eq("route_id", vo.getRouteId()));
            for (InspectionPointItem item : items) {
                InspectionRoutePoint inspectionRoutePoint = inspectionRoutePointMapper.selectOne(new QueryWrapper<InspectionRoutePoint>()
                        .eq("point_id", patrolPoint.getId()).eq("route_id", vo.getRouteId()));
                InspectionItemManagement inspectionItemManagement = inspectionItemManagementMapper.selectById(item.getItemId());
                if (item.getRoutePointId().equals(inspectionRoutePoint.getId())) {
                    InspectionItemManagementExtend extend = BeanCopyUtil.copyObject(inspectionItemManagement, InspectionItemManagementExtend.class);
                    extend.setPointId(inspectionPointManagementExtend.getId());
                    extend.setId(null);
                    inspectionItemManagementExtendMapper.insert(extend);
                }
            }
            if (patrolPoint.getType()==3) {
                InspectionItemManagementExtend extend = new InspectionItemManagementExtend();
                extend.setPointId(inspectionPointManagementExtend.getId());
                extend.setName(patrolPoint.getName());
                inspectionItemManagementExtendMapper.insert(extend);
            }
        }
//        List<InspectionPointManagementExtend> extendList = BeanCopyUtil.copyList(patrolPoints, InspectionPointManagementExtend.class);
//        extendList.stream().forEach(extend -> extend.setId(null));
//        extendList.stream().forEach(extend -> extend.setPlanId(vo.getId()));
//        extendList.stream().forEach(extend -> inspectionPointManagementExtendMapper.insert(extend));
//        List<InspectionPointItem> items = inspectionPointItemMapper.selectList(new QueryWrapper<InspectionPointItem>()
//                .eq("route_id", vo.getRouteId()));
//        List<InspectionItemManagement> inspectionItemManagements = inspectionItemManagementMapper
//                .selectBatchIds(items.stream().map(InspectionPointItem::getItemId).collect(Collectors.toList()));
//        List<InspectionItemManagementExtend> anExtends = BeanCopyUtil.copyList(inspectionItemManagements, InspectionItemManagementExtend.class);
//        anExtends.stream().forEach(inspectionItemManagementExtend -> inspectionItemManagementExtend.setId(null));
//        anExtends.stream().forEach(extend -> inspectionItemManagementExtendMapper.insert(extend));
        for (Long userId : vo.getUserIds()) {
            PersonnelRelations person =  new PersonnelRelations();
            person.setUserId(userId);
            person.setOtherId(vo.getId());
            personnelRelationsMapper.insert(person);
            Message message = new Message();
            message.setType(2);
            message.setUserId(userId);
            message.setTitle("尊敬的用户，有新的巡检信息已生成，请尽快受理并处理！！！");
            message.setIsReceive(0);
            messageMapper.insert(message);
        }
    }
}
